﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

public partial class userControls_MovieList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        populateControls();
    }

    private void populateControls()
    {
        int genreId = Int32.Parse(Request.QueryString["genre"]);
        int page;
        try
        {
            page = Int32.Parse(Request.QueryString["page"]);
        }
        catch(Exception ex)
        {
            page = 0;
        }

        if (page == 0)
        {
            page = 1;
        }
        int howManyPages = 1;

        if (ValidGenre(genreId))
        {
            list.DataSource = MovieAccess.GetMoviesInGenre(genreId, page, out howManyPages, 0);
            list.DataBind();
        }

        Paging(howManyPages, page);
    }

    private void Paging(int howManyPages, int page)
    {
        if (howManyPages > 1)
        {
            // have the current page as integer
            int currentPage = page;
            // make controls visible
            pagingLabel.Visible = true;
            previousLink.Visible = true;
            nextLink.Visible = true;
            // set the paging text
            pagingLabel.Text = " " + page + " од " + howManyPages.ToString() + " ";
            // create the Previous link
            if (currentPage == 1)
                previousLink.Enabled = false;
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                    if (query.AllKeys[i] != null)
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString += paramName + "=" + query[i] + "&";
                previousLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString + "Page=" + (currentPage - 1).ToString();
            }
            // create the Next link
            if (currentPage == howManyPages)
                nextLink.Enabled = false;
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                    if (query.AllKeys[i] != null)
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString += paramName + "=" + query[i] + "&";
                nextLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString + "Page=" + (currentPage + 1).ToString();
            }
        }
    }
    
    private bool ValidGenre(int genre)
    {
        if (genre != 4 && genre != 5 && genre != 6 && genre != 7 && genre != 8 && genre != 9 && genre != 10 && genre != 11 && genre != 12) return false;
        return true;
    }
}
